/*
 * 文件名：HttpUtil.java	 
 * 时     间：下午11:12:18
 * 作     者：Administrator       
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.local.util;

import java.io.IOException;
import java.nio.charset.UnsupportedCharsetException;
import java.text.SimpleDateFormat;

import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

import com.cz073586.tools.http.HttpManagerUtil;
import com.cz073586.tools.util.JsonUtil;
import com.local.entity.vo.CarParkInVo;
import com.local.entity.vo.CarParkOutVo;
import com.local.entity.vo.ParkwellMemberVo;
import com.local.entity.vo.PayBillVo;
import com.local.entity.vo.ReqeustEntity;
import com.local.entity.vo.ResultVo;
import com.local.manager.cfgManager.ConfigManager;
import com.local.model.CarParkIn;
import com.local.model.CarParkOut;
import com.local.model.LocalMember;
import com.local.model.LocalPayBill;

/**
 * 类描述:这里是类描述
 * 
 * @ClassName: HttpUtil
 * @author binghen
 * @date 2015年4月2日 下午11:12:18
 */
public class HttpUtil
{
	private HttpUtil()
	{
		
	}
	
	/**
	 * @throws IOException
	 * @throws ClientProtocolException
	 * @throws UnsupportedEncodingException
	 *             方法描述： 车辆入场与后台通信,后台错误代码放在reqeust里
	 * @Title: sendHttpReqeustForIn
	 * @param @param request
	 * @param @param deviceNumber
	 * @param @return
	 * @return boolean
	 * @user Administrator 2015年4月2日
	 * @throws
	 */
	public static boolean sendHttpReqeustForIn(ReqeustEntity request,
			CarParkIn carParkIn) throws ClientProtocolException, IOException
	{
		CloseableHttpClient httpClient = HttpManagerUtil.getInstance()
				.getHttpClient(null);
		
		try
		{
			HttpPost post = new HttpPost(ConfigManager.instance().getCarInUri());
			// 设置超时时间
			post.setConfig(HttpClientUtil.getTimeOutCfg(0));
			post.setHeader("Content-Type", "application/json;charset=UTF-8");
			CarParkInVo carIn = new CarParkInVo();
			carIn.setUuid(carParkIn.getUuid());
			carIn.setEquipment(carParkIn.getEquipment());
			carIn.setExchangeUuid(carParkIn.getExchangeUuid());
			carIn.setCarNo(carParkIn.getCarNo());
			carIn.setOpenTime(carParkIn.getOpenTime());
			String sendData = JsonUtil.getJsonString(carIn);
			sendData = EncodingUtil.ConvertEncodingToUtf8(sendData);
			StringEntity s = new StringEntity(sendData, "UTF-8");
			
			post.setEntity(s);
			CloseableHttpResponse resp = httpClient.execute(post);
			String result = EntityUtils.toString(resp.getEntity());
			ResultVo resultVo = (ResultVo) JsonUtil.getObjectByJson(result,
					ResultVo.class);
			if (resultVo.getResult().equals("success"))
			{
				return true;
			} else if (resultVo.getResult().equals("error"))
			{
				String errMsg = new String(resultVo.getErrorMsg().getBytes(
						"iso-8859-1"), "utf-8");
				request.setErrorCode(resultVo.getErrorCode());
				request.setError(errMsg);
			}
		} catch (UnsupportedCharsetException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally
		{
			
			httpClient.close();
		}
		
		return false;
	}
	
	/**
	 * 
	 * 方法描述： 上传进场车牌
	 * 
	 * @Title: sendHttpReqeustForOut
	 * @param @param request
	 * @param @param deviceNumber
	 * @param @return
	 * @param @throws ClientProtocolException
	 * @param @throws IOException
	 * @return boolean
	 * @user Administrator 2015年4月12日
	 * @throws
	 */
	public static boolean sendHttpReqeustForOut(ReqeustEntity request,
			CarParkOut carParkOut) throws ClientProtocolException, IOException
	{
		CloseableHttpClient httpClient = HttpManagerUtil.getInstance()
				.getHttpClient(null);
		
		try
		{
			HttpPost post = new HttpPost(ConfigManager.instance()
					.getCarOutUri());
			
			// 设置超时时间
			post.setConfig(HttpClientUtil.getTimeOutCfg(0));
			post.setHeader("Content-Type", "application/json;charset=UTF-8");
			CarParkOutVo carOut = new CarParkOutVo();
			carOut.setUuid(carParkOut.getUuid());
			carOut.setEquipment(carParkOut.getEquipment());
			carOut.setExchangeUuid(carParkOut.getExchangeUuid());
			carOut.setCarNo(carParkOut.getCarNo());
			carOut.setOpenTime(carParkOut.getOpenTime());
			carOut.setCarInUuid(carParkOut.getCarInUuid());
			String sendData = JsonUtil.getJsonString(carOut);
			sendData = EncodingUtil.ConvertEncodingToUtf8(sendData);
			StringEntity s = new StringEntity(sendData, "UTF-8");
			post.setEntity(s);
			CloseableHttpResponse resp = httpClient.execute(post);
			String result = EntityUtils.toString(resp.getEntity());
			ResultVo resultVo = (ResultVo) JsonUtil.getObjectByJson(result,
					ResultVo.class);
			if (resultVo.getResult().equals("success"))
			{
				if (resultVo.getIsOpen().equals("true"))
				{
					return true;
				}
				return false;
			} else if (resultVo.getResult().equals("error"))
			{
				String errMsg = new String(resultVo.getErrorMsg().getBytes(
						"iso-8859-1"), "utf-8");
				request.setErrorCode(resultVo.getErrorCode());
				request.setError(errMsg);
			}
		} catch (UnsupportedCharsetException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally
		{
			httpClient.close();
		}
		
		return false;
	}
	
	/**
	 * 
	 * 方法描述：上传会员
	 * 
	 * @Title: sendHttpReqeustForOut
	 * @param @param request
	 * @param @param localMember
	 * @param @return
	 * @param @throws ClientProtocolException
	 * @param @throws IOException
	 * @return boolean
	 * @user PiChunhan 2015年7月14日
	 * @throws
	 */
	public static boolean sendHttpReqeustForMember(ReqeustEntity request,
			LocalMember localMember) throws ClientProtocolException,
			IOException
	{
		CloseableHttpClient httpClient = HttpManagerUtil.getInstance()
				.getHttpClient(null);
		
		HttpPost post = new HttpPost(ConfigManager.instance()
				.getAddParkwellMemberUri());
		
		try
		{
			// 设置超时时间
			post.setConfig(HttpClientUtil.getTimeOutCfg(0));
			post.setHeader("Content-Type", "application/json;charset=UTF-8");
			ParkwellMemberVo parkwellMemberVo = new ParkwellMemberVo();
			parkwellMemberVo.setCardUuid(localMember.getCardUuid());
			parkwellMemberVo.setCarNo(localMember.getCarNo());
			parkwellMemberVo.setCarTypeUuid(localMember.getCarTypeUuid());
			parkwellMemberVo.setCreateTime(localMember.getCreateTime());
			parkwellMemberVo.setCreateType(ConfigManager.instance()
					.getParkName());
			SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
			parkwellMemberVo
					.setStartTime(sdf.format(localMember.getStartTime()));
			parkwellMemberVo.setMemberName(localMember.getMemberName());
			String sendData = JsonUtil.getJsonString(parkwellMemberVo);
			sendData = EncodingUtil.ConvertEncodingToUtf8(sendData);
			StringEntity s = new StringEntity(sendData, "UTF-8");
			post.setEntity(s);
			CloseableHttpResponse resp = httpClient.execute(post);
			String result = EntityUtils.toString(resp.getEntity());
			ResultVo resultVo = (ResultVo) JsonUtil.getObjectByJson(result,
					ResultVo.class);
			if (resultVo.getResult().equals("success"))
			{
				if (resultVo.getIsOpen().equals("true"))
				{
					return true;
				}
				return false;
			} else if (resultVo.getResult().equals("error"))
			{
				String errMsg = new String(resultVo.getErrorMsg().getBytes(
						"iso-8859-1"), "utf-8");
				request.setErrorCode(resultVo.getErrorCode());
				request.setError(errMsg);
			}
		} catch (UnsupportedCharsetException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally
		{
			httpClient.close();
		}
		
		return false;
	}
	
	public static boolean sendHttpReqeustForPayBill(ReqeustEntity request,
			LocalPayBill localPayBill) throws ClientProtocolException,
			IOException
	{
		CloseableHttpClient httpClient = HttpManagerUtil.getInstance()
				.getHttpClient(null);
		
		try
		{
			HttpPost post = new HttpPost(ConfigManager.instance()
					.getAddPayBillUri());
			
			// 设置超时时间
			post.setConfig(HttpClientUtil.getTimeOutCfg(0));
			post.setHeader("Content-Type", "application/json;charset=UTF-8");
			PayBillVo payBillVo = new PayBillVo();
			payBillVo.setCarNo(localPayBill.getCarNo());
			payBillVo.setParkInUuid(localPayBill.getParkInUuid());
			payBillVo.setPayTime(localPayBill.getPayTime());
			payBillVo.setPayType(localPayBill.getPayType());
			payBillVo.setPrice(localPayBill.getPrice());
			String sendData = JsonUtil.getJsonString(payBillVo);
			sendData = EncodingUtil.ConvertEncodingToUtf8(sendData);
			StringEntity s = new StringEntity(sendData, "UTF-8");
			post.setEntity(s);
			CloseableHttpResponse resp = httpClient.execute(post);
			String result = EntityUtils.toString(resp.getEntity());
			ResultVo resultVo = (ResultVo) JsonUtil.getObjectByJson(result,
					ResultVo.class);
			if (resultVo.getResult().equals("success"))
			{
				if (resultVo.getIsOpen().equals("true"))
				{
					return true;
				}
				return false;
			} else if (resultVo.getResult().equals("error"))
			{
				String errMsg = new String(resultVo.getErrorMsg().getBytes(
						"iso-8859-1"), "utf-8");
				request.setErrorCode(resultVo.getErrorCode());
				request.setError(errMsg);
			}
		} catch (UnsupportedCharsetException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally
		{
			httpClient.close();
		}
		return false;
	}
	
}
